home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / modex32w.zip / MODE.ASM < prev    next >
Assembly Source File  |  1995-02-21  |  5KB  |  146 lines

  1.         .386p
  2.         locals
  3.         include w.inc
  4.  
  5. ; Mode X (320x240, 256 colors) mode set routine. Works on all VGAs.
  6. ; C near-callable as:
  7. ;       void Set320x240Mode(unsigned scrwidth);
  8. ; Tested with TASM 2.0.
  9. ; Modified from public-domain mode set code by John Bridges.
  10. ;
  11. ; Modified Aug 30, 1994 (ykumanan)
  12. ; ) Made code 32 bit pm (not fully optimized)
  13. ;
  14.  
  15. SC_INDEX equ    03c4h   ;Sequence Controller Index
  16. CRTC_INDEX equ  03d4h   ;CRT Controller Index
  17. MISC_OUTPUT equ 03c2h   ;Miscellaneous Output register
  18. CSCREEN_SEG equ 0a000h  ;segment of display memory in mode X
  19. CRTC_OFFS equ 13h       ;offset
  20.  
  21. parms   struc
  22.         dd      2 dup (?) ;pushed EBP and return address
  23. ScrWid  dd      ?         ;reqstd width for scan line
  24. parms   ends
  25.  
  26.         @dseg
  27.  
  28.         public  SCREEN_SEG, SCREEN_WIDTH
  29.  
  30. ; Selector for the video segment
  31. SCREEN_SEG      dd      0
  32. SCREEN_WIDTH    dd      080h
  33.  
  34. ; Index/data pairs for CRT Controller registers that differ between
  35. ; mode 13h and mode X.
  36. CRTParms label  word
  37.         dw      00d06h  ;vertical total
  38.         dw      03e07h  ;overflow (bit 8 of vertical counts)
  39.         dw      04109h  ;cell height (2 to double-scan)
  40.         dw      0ea10h  ;v sync start
  41.         dw      0ac11h  ;v sync end and protect cr0-cr7
  42.         dw      0df12h  ;vertical displayed
  43.         dw      00014h  ;turn off dword mode
  44.         dw      0e715h  ;v blank start
  45.         dw      00616h  ;v blank end
  46.         dw      0e317h  ;turn on byte mode
  47. CRT_PARM_LENGTH equ     (($-CRTParms)/2)
  48.  
  49.         ends
  50.  
  51.         @cseg
  52.  
  53.         public  _Set320x240Mode
  54.  
  55. _Set320x240Mode proc
  56.         push    ebp      ;preserve caller's stack frame
  57.         mov     ebp, esp
  58.         push    esi      ;preserve C register vars
  59.         push    edi      ; (don't count on BIOS preserving anything)
  60.         push    ebx
  61.  
  62.         ;; make SCREEN_SEG first
  63.         mov     eax, CSCREEN_SEG shl 4
  64.         mov     SCREEN_SEG, eax
  65.         ;; done
  66.  
  67.         mov     ax,13h  ;let the BIOS set standard 256-color
  68.         int     10h     ; mode (320x200 linear)
  69.  
  70.         mov     dx,SC_INDEX
  71.         mov     ax,0604h
  72.         out     dx,ax   ;disable chain4 mode
  73.         mov     ax,0100h
  74.         out     dx,ax   ;synchronous reset while switching clocks
  75.  
  76.         mov     dx,MISC_OUTPUT
  77.         mov     al,0e3h
  78.         out     dx,al   ;select 28 MHz dot clock & 60 Hz scanning rate
  79.  
  80.         mov     dx,SC_INDEX
  81.         mov     ax,0300h
  82.         out     dx,ax   ;undo reset (restart sequencer)
  83.  
  84.         mov     dx,CRTC_INDEX ;reprogram the CRT Controller
  85.         mov     al,11h  ;VSync End reg contains register write
  86.         out     dx,al   ; protect bit
  87.         inc     dx      ;CRT Controller Data register
  88.         in      al,dx   ;get current VSync End register setting
  89.         and     al,7fh  ;remove write protect on various
  90.         out     dx,al   ; CRTC registers
  91.         dec     dx      ;CRT Controller Index
  92.         cld
  93.         mov     esi,offset CRTParms ;point to CRT parameter table
  94.         mov     ecx,CRT_PARM_LENGTH ;# of table entries
  95. SetCRTParmsLoop:
  96.         lodsw           ;get the next CRT Index/Data pair
  97.         out     dx,ax   ;set the next CRT Index/Data pair
  98.         loop    SetCRTParmsLoop
  99.  
  100.         mov     dx,SC_INDEX
  101.         mov     ax,0f02h
  102.         out     dx,ax   ;enable writes to all four planes
  103.         mov     edi, [SCREEN_SEG] ; point es:edi to disp mem
  104.         sub     ax,ax   ;clear to zero-value pixels
  105.         mov     ecx,8000h ;# of words in display memory
  106.         rep     stosw   ;clear all of display memory
  107.  
  108.         ;; set screen width to allow for horizontal panning
  109.         mov     dx, CRTC_INDEX
  110.         mov     al, CRTC_OFFS
  111.         out     dx, al
  112.         inc     dx
  113.  
  114.         mov     eax, [ebp+ScrWid]
  115.         cmp     eax, 320                ; is it >= screen width?
  116.         jge     @@valid
  117.         mov     eax, 320
  118. @@valid:
  119.         shr     eax, 3                  ; div by 8 to make it work
  120.         out     dx, al                  ; set it
  121.  
  122.         shl     eax, 1
  123.         mov     [SCREEN_WIDTH], eax     ; # of bytes, not words
  124. @@bye:
  125.         pop     ebx
  126.         pop     edi     ;restore C register vars
  127.         pop     esi
  128.         pop     ebp      ;restore caller's stack frame
  129.         ret
  130. _Set320x240Mode endp
  131.  
  132.         public  _ResetModeX
  133. _ResetModeX     proc
  134.         push    ebp
  135.  
  136.         mov     ax, 3
  137.         int     10h                     ; text mode
  138.  
  139.         pop     ebp
  140.         ret
  141. _ResetModeX     endp
  142.  
  143.         ends
  144.         end
  145.  
  146.